﻿#include "Shinsu.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool Shinsu::getCanAttack() const
    {
        return MonsterObject::getCanAttack() && Mode;
    }

    Shinsu::Shinsu(MonsterInfo *info) : MonsterObject(info)
    {
        ActionTime = getEnvir()->getTime() + 1000;
    }

    void Shinsu::ProcessAI()
    {
        if (!Dead && getEnvir()->getTime() > ActionTime)
        {
            if (getTarget() != nullptr)
            {
                ModeTime = getEnvir()->getTime() + 30000;
            }

            if (!Mode && getEnvir()->getTime() < ModeTime)
            {
                Mode = true;
                S::ObjectShow *tempVar = new S::ObjectShow();
                tempVar->ObjectID = ObjectID;
                Broadcast(tempVar);
                ActionTime = getEnvir()->getTime() + 1000;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else if (Mode && getEnvir()->getTime() > ModeTime)
            {
                Mode = false;
                S::ObjectHide *tempVar2 = new S::ObjectHide();
                tempVar2->ObjectID = ObjectID;
                Broadcast(tempVar2);
                ActionTime = getEnvir()->getTime() + 1000;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }

        MonsterObject::ProcessAI();
    }

    bool Shinsu::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }


        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    void Shinsu::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);

        LineAttack(2);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void Shinsu::Spawned()
    {
        MonsterObject::Spawned();

        Summoned = true;
    }

    Packet *Shinsu::GetInfo()
    {
        S::ObjectMonster *tempVar = new S::ObjectMonster();
        tempVar->ObjectID = ObjectID;
        tempVar->Name = getName();
        tempVar->NameColour = NameColour;
        tempVar->Location = getCurrentLocation();
        tempVar->Image = Mode ? Monster::Shinsu1 : Monster::Shinsu;
        tempVar->Direction = getDirection();
        tempVar->Effect = Info->Effect;
        tempVar->AI = Info->AI;
        tempVar->Light = Info->Light;
        tempVar->Dead = Dead;
        tempVar->Skeleton = Harvested;
        tempVar->Poison = CurrentPoison;
        tempVar->Hidden = getHidden();
        tempVar->Extra = Summoned;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was used in a 'return' or 'throw' statement.
        return tempVar;
    }
}
